package com.jianguozh.difs.support.api.ops;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.jianguozh.difs.common.bean.R;
import com.jianguozh.difs.framework.base.BaseController;
import com.jianguozh.difs.support.api.ops.vo.AppEnumItemVo;
import com.jianguozh.difs.support.api.ops.vo.AppEnumVo;
import com.jianguozh.difs.support.service.utils.EnumUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应用标准 前端控制器
 *
 * @author hanrs
 * @email 4076130@qq.com
 * @since 2022/6/17 15:39
 */
@RestController
public class AppEnumController extends BaseController {


    @Autowired
    MybatisPlusProperties mybatisPlusProperties;

    /**
     * 获取所有对象
     */
    @GetMapping("/o/enum/all")
    public R<Map> all() {
        String packageName = mybatisPlusProperties.getTypeEnumsPackage();
        Map<String, Map> map = EnumUtils.getEnums(packageName);
        Map<String, Map> items = new HashMap<>();
        map.forEach((k, v) -> {
            items.put(k, (Map) v.get("list"));
        });
        return success(items);
    }

    /**
     * 获取所有对象
     */
    @GetMapping("/appenum/list")
    public R<List> list() {
        String packageName = mybatisPlusProperties.getTypeEnumsPackage();
        Map<String, Map> map = EnumUtils.getEnums(packageName);
        List<AppEnumVo> list = new ArrayList();
        map.forEach((k, v) -> {
            AppEnumVo appEnumVo = new AppEnumVo();
            appEnumVo.setCode(k);
            appEnumVo.setTitle((String) v.get("title"));

            Map values = (Map) v.get("list");
            List listValues = new ArrayList();
            values.forEach((key, value) -> {
                AppEnumItemVo itemVo = new AppEnumItemVo();
                itemVo.setValue((String) key);
                itemVo.setDisplayName((String) value);
                listValues.add(itemVo);
            });
            appEnumVo.setItems(listValues);
            list.add(appEnumVo);
        });
        return success(list);
    }


}
